//
//  VIdeoDetailViewController.m
//  ErWa
//
//  Created by 王阳阳 on 16/3/12.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "VIdeoDetailViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
@interface VIdeoDetailViewController ()
@property(nonatomic, strong)MPMoviePlayerController *moviePlayer;//视频播放控制器
@end

@implementation VIdeoDetailViewController

- (void)dealloc
{
    //移除所有通知监控
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    //右侧登陆按钮
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(black)];
    
    //开始播放
    [self.moviePlayer play];
    
    //添加通知
    [self addNotification];
    
    //获取缩略图
//    [self thumbnaiImageRequest];
}
#pragma mark - 私有方法
//获得本地文件路径
-(NSURL *)getFileUrl{
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"" ofType:nil];
    return [NSURL URLWithString:urlStr];
}
//获取网络文件路径
-(NSURL *)getNetworkUrl{
    NSString *urlStr = self.urlStr;
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return [NSURL URLWithString:urlStr];
}
//创建媒体播放器
-(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
        NSURL *url = [self getNetworkUrl];
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        _moviePlayer.view.frame = self.view.bounds;
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}
//获取视频缩略图
-(void)thumbnaiImageRequest{
    //获取13.0s、21.5s的缩略图
    [_moviePlayer requestThumbnailImagesAtTimes:@[@13.0, @21.5] timeOption:MPMovieTimeOptionNearestKeyFrame];
}

#pragma mark - 控制器通知
//添加通知监控多媒体控制其状态
-(void)addNotification{
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerThumbnailRequestFinished:) name:MPMoviePlayerThumbnailImageRequestDidFinishNotification object:self.moviePlayer];
    
    //监听屏幕旋转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarPositionChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
//屏幕旋转方法实现
- (void)statusBarPositionChange:(NSNotification *)noti
{
    //使用代码监听横竖屏
    //获取状态栏方向
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    //判断
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"向左横屏");
        [self landscape];
    }else if (orientation == UIInterfaceOrientationLandscapeRight){
        NSLog(@"向右横屏");
        [self landscape];
    }else if (orientation == UIInterfaceOrientationPortrait){
        NSLog(@"竖屏");
        [self portrait];
    }
}
#pragma mark - 横屏设置
- (void)landscape{
    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark - 竖屏设置
- (void)portrait
{
    self.navigationController.navigationBar.hidden = NO;
    
}
//播放状态改变，注意播放完成时的状态是暂停
- (void)mediaPlayerPlaybackStateChange:(NSNotification *)notify{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放");
            break;
            
        default:
            NSLog(@"播放状态：%ld",(long)self.moviePlayer.playbackState);
            break;
    }
}
//播放完成
- (void)mediaPlayerPlaybackFinished:(NSNotification *)notify{
    NSLog(@"播放完成");
}
//缩略图请求完成，此方法每次截图成功后都会调用一次
- (void)mediaPlayerThumbnailRequestFinished:(NSNotification *)notify{
    NSLog(@"视频截图成功");
    UIImage *image = notify.userInfo[MPMoviePlayerThumbnailImageKey];
    //保存图片到相册（首次调用会请求用户获访问相册权限）
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
}

- (void)black
{
    
    //停止播放
    [self.moviePlayer stop];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
